package com.qiqi.ctbluetooth.permission

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions
import com.hjq.permissions.permission.PermissionLists
import com.hjq.permissions.permission.base.IPermission


fun FragmentActivity.requestBluePermissions(onResult: (grantedList: List<IPermission?>, deniedList: List<IPermission?>) -> Unit) {
    requestBluePermissions(this, onResult)
}

fun FragmentActivity.requestCameraPermissions(onResult: (grantedList: List<IPermission?>, deniedList: List<IPermission?>) -> Unit) {
    requestCameraPermissions(this, onResult)
}



fun Fragment.requestBluePermissions(onResult: (grantedList: List<IPermission?>, deniedList: List<IPermission?>) -> Unit) {
    requestBluePermissions(requireContext(), onResult)
}




    fun requestBluePermissions(context: Context, onResult: (grantedList: List<IPermission?>, deniedList: List<IPermission?>) -> Unit) {

//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
//            PopTip.show( StringUtils.getString(R.string.demo_android_12_bluetooth_permission_hint));
//        }

        XXPermissions.with(context)
            .permission(PermissionLists.getBluetoothScanPermission())
            .permission(PermissionLists.getBluetoothConnectPermission())
            .permission(PermissionLists.getBluetoothAdvertisePermission())
            .interceptor(PermissionInterceptor())
            .description(PermissionDescription())
            .request(object : OnPermissionCallback {
                override fun onResult(grantedList: List<IPermission?>, deniedList: List<IPermission?>) {
                    var allGranted = deniedList.isEmpty();
                    if (!allGranted) {
                        LogUtils.i("权限申请失败==>:$deniedList")
                        ToastUtils.showShort("获取部分权限成功，但部分权限未正常授予==》$deniedList")
                        return;
                    }
                    onResult.invoke(grantedList,deniedList)
                }
            })
    }


fun requestCameraPermissions(context: Context, onResult: (grantedList: List<IPermission?>, deniedList: List<IPermission?>) -> Unit) {

//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
//            PopTip.show( StringUtils.getString(R.string.demo_android_12_bluetooth_permission_hint));
//        }

    XXPermissions.with(context)
//            .permission(PermissionLists.getReadExternalStoragePermission())
//            .permission(PermissionLists.getWriteExternalStoragePermission())
        .permission(PermissionLists.getManageExternalStoragePermission())
        .permission(PermissionLists.getCameraPermission())
        .interceptor(PermissionInterceptor())
        .description(PermissionDescription())
        .request(object : OnPermissionCallback {
            override fun onResult(grantedList: List<IPermission?>, deniedList: List<IPermission?>) {
                var allGranted = deniedList.isEmpty();
                if (!allGranted) {
                    LogUtils.i("权限申请失败==>:$deniedList")
                    ToastUtils.showShort("获取部分权限成功，但部分权限未正常授予==》$deniedList")
                    return;
                }
                onResult.invoke(grantedList,deniedList)
            }
        })
}


